package com.copa.l976;

import java.util.Arrays;

/**
 * @author copa
 * @createDate 2022-08-29 11:24
 * @function 排序法
 */
public class Test1 {

    public int largestPerimeter(int[] nums) {
        Arrays.sort(nums);
        int maxSideIndex = nums.length - 1;
        int side1 = nums.length - 3;
        int side2 = nums.length - 2;
        for (int i = side1; i >= 0; i--) {
            if (checkTri(nums[side1], nums[side2], nums[maxSideIndex])) {
                break;
            }
            maxSideIndex--;
            side1--;
            side2--;
        }
        return side1 == -1 ? 0 : nums[side1] + nums[side2] + nums[maxSideIndex];
    }

    /**
     * 检测是否可以构成三角形
     * @param a 边1
     * @param b 边2
     * @param c 最大边
     * @return
     */
    public Boolean checkTri(int a, int b, int c) {
        return a + b > c;
    }

    public static void main(String[] args) {
        int[] nums = {2,1,2};
        System.out.println(new Test1().largestPerimeter(nums));
    }
}
